package com.example.partner.activity.home;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager2.widget.ViewPager2;

import com.example.partner.adapter.user.UserViewPagerAdapter;
import com.example.partner.databinding.FragmentHomeBinding;
import com.example.partner.fragment.CommonFragment;
import com.example.partner.fragment.MatchFragment;
import com.example.partner.fragment.NearFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class HomeFragment extends Fragment  {

    private FragmentHomeBinding binding;

    ViewPager2 viewPager;
    TabLayout tabLayout;

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        Log.d("FragmentLifecycle", "onAttach: Attached to Activity");
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();
        viewPager = binding.viewPager;
        tabLayout =binding.tabLayout;
        return root;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 在这里进行与 Activity 相关的操作
        UserViewPagerAdapter viewPagerAdapter = new UserViewPagerAdapter(getActivity());
        viewPager.setAdapter(viewPagerAdapter);
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            // 设置标签的文本
            switch (position) {
                case 0:
                    tab.setText("推荐用户");
                    break;
                case 1:
                    tab.setText("智能匹配");
                    break;
                case 2:
                    tab.setText("附近用户");
                    break;
            }
        }).attach();

        // 添加 TabLayout 的 Tab 选择监听器
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Fragment selectedFragment = null;
                switch (tab.getPosition()) {
                    case 0:
                        selectedFragment = new CommonFragment();
                        break;
                    case 1:
                        selectedFragment = new MatchFragment();
                        break;
                    case 2:
                        selectedFragment = new NearFragment();

                        break;
                }
                if (selectedFragment != null) {
                    // 使用 FragmentManager 来切换 Fragment
                    //getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
    }


}